package it.unisa.ingegneria.tdp.esercitazione5;

import org.junit.*;

import static org.junit.Assert.*;

public class TestGraph {
	private Graph<String> g;
	private BFS<String> bfs;
	private Kruskal<String> kruskal;

	@Before
	public void setUp() {
		g = new Graph<String>();
		bfs = new BFS<String>(g);
		kruskal = new Kruskal<String>(g);

	}
	
	/* Test del grafo vuoto*/
	@Test
	public void TestGraphEmpty() {
		assertEquals(0, g.numVertices());
		assertEquals(0, g.numEdges());
	}
	
	/* Test del grafo con due elementi*/
	@Test
	public void TestGraphTwoElement(){
		MyVertex<String> v1= g.insertVertex("a");
		MyVertex<String> v2= g.insertVertex("b");

		g.insertEdge(v1, v2); 
		String s = v1.toString() + " -- " + v2.toString() + " -- " + "\n" + v2.toString() + " -- " + "\n";
		assertEquals(g.toString(), s);
	}
	
	/* Test del grafo con tre elementi*/
	@Test
	public void TestGraphTreeElement(){
		MyVertex<String> v1= g.insertVertex("a");
		MyVertex<String> v2= g.insertVertex("b");
		MyVertex<String> v3= g.insertVertex("c");

		g.insertEdge(v1, v2);
		g.insertEdge(v2, v3);
		String s = v1.toString() + " -- " + v2.toString() + " -- " + "\n" + v2.toString() + " -- " +  v3.toString() + " -- " + "\n" +  v3.toString() + " -- " + "\n";
		assertEquals(g.toString(), s);
	}
	
	/* Test della visita in ampiezza(BFT)*/
	@Test
	public void TestBft(){
		MyVertex<String> v1= g.insertVertex("a");
		MyVertex<String> v2= g.insertVertex("f");
		MyVertex<String> v3= g.insertVertex("d");
		MyVertex<String> v4= g.insertVertex("b");
		MyVertex<String> v5= g.insertVertex("g");
		MyVertex<String> v6= g.insertVertex("c");
		MyVertex<String> v7= g.insertVertex("e");
		MyVertex<String> v8= g.insertVertex("h");
		MyVertex<String> v9= g.insertVertex("i");
		MyVertex<String> v10= g.insertVertex("l");


		g.insertEdge(v1, v2);  
		g.insertEdge(v1, v6);         
		g.insertEdge(v2, v3);    
		g.insertEdge(v3, v4);  
		g.insertEdge(v3, v5);
		g.insertEdge(v6, v7);
		g.insertEdge(v6, v8);
		g.insertEdge(v1, v9);
		g.insertEdge(v4, v10);

		String s = "afciehdbgl";
		assertEquals(bfs.breadthFirstSearch(v1), s);
	}
	
	/* Test dell'algoritmo di Kruskal*/
	@Test
	public void TestKruskal(){
		
		MyVertex<String> v1= g.insertVertex("a");
		MyVertex<String> v2= g.insertVertex("h");
		MyVertex<String> v3= g.insertVertex("b");
		MyVertex<String> v4= g.insertVertex("i");
		MyVertex<String> v5= g.insertVertex("g");
		MyVertex<String> v6= g.insertVertex("c");
		MyVertex<String> v7= g.insertVertex("f");
		MyVertex<String> v8= g.insertVertex("d");
		MyVertex<String> v9= g.insertVertex("e");
		
		g.insertEdge(v1, v2,7);  
		g.insertEdge(v1, v3,4);         
		g.insertEdge(v2, v3,11);    
		g.insertEdge(v2, v4,8);  
		g.insertEdge(v2, v5,1);
		g.insertEdge(v3, v6,8);
		g.insertEdge(v4, v6,2);
		g.insertEdge(v4, v5,6);
		g.insertEdge(v5, v7,2);  
		g.insertEdge(v6, v8,7);         
		g.insertEdge(v6, v7,4);    
		g.insertEdge(v7, v9,10);  
		g.insertEdge(v8, v7,14);
		g.insertEdge(v8, v9,1);
		
		Graph<String> gk = kruskal.creaAlberoDiConnessioneMinimo();
		String s = "ahbhbigbcicggfcdffedfee";		            
		System.out.println(gk.stampaTest());
		assertEquals(gk.stampaTest(), s);
	}

	@Test
	public void TestNumVertices(){
		g.insertVertex("a");
		g.insertVertex("b");
		g.insertVertex("c");

		assertEquals(3, g.numVertices());

	}

	@Test
	public void TestNumEdges(){
		MyVertex<String> v1= g.insertVertex("a");
		MyVertex<String> v2= g.insertVertex("b");
		MyVertex<String> v3= g.insertVertex("c");

		g.insertEdge(v1, v2);
		g.insertEdge(v2, v3);
		
		assertEquals(2, g.numEdges());
	}
	
	@Test
	public void TestOutgoingEdges(){
		MyVertex<String> v1= g.insertVertex("a");
		MyVertex<String> v2= g.insertVertex("b");
		MyVertex<String> v3= g.insertVertex("c");
		MyVertex<String> v4= g.insertVertex("d");
		
		g.insertEdge(v1, v2);
		g.insertEdge(v1, v3);
		g.insertEdge(v1, v4);
		
		String s = v2.toString() + v3.toString() + v4.toString();
		assertEquals(g.testOutgoingEdges(v1), s);
	}
	
	@Test
	public void TestVertices(){
		MyVertex<String> v1= g.insertVertex("a");
		MyVertex<String> v2= g.insertVertex("b");
		MyVertex<String> v3= g.insertVertex("c");
		MyVertex<String> v4= g.insertVertex("d");
		
		String s = v1.toString() + v2.toString() + v3.toString() + v4.toString();
		assertEquals(g.TestVertices(), s);
	}
	
}
